package com.gmail.dengtao.joe.group.channel;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gmail.dengtao.joe.group.channel.Channel;
import com.gmail.dengtao.joe.group.member.IdleStatus;
import com.gmail.dengtao.joe.group.member.Member;
import com.gmail.dengtao.joe.group.member.MembershipListener;

public class MyMembershipListener implements MembershipListener {

	private static Logger LOGGER = LoggerFactory.getLogger(MyMembershipListener.class);

	@Override
	public void memberAdded(Channel channel, Member member) throws Exception {
		LOGGER.info("[NEW " + member + "]");
		// channel.send(member, "你好.".getBytes());
		channel.broadcast(("NEW MEMBER - /" + member.getHost() + ":" + member.getPort()).getBytes());
	}

	@Override
	public void memberIdle(Channel channel, Member member, IdleStatus status) throws Exception {
		//LOGGER.warn("[IDLE " + member + "] IdleStatus [" + status + "]");
		//channel.send(member, "我还在.".getBytes());
	}

	@Override
	public void memberDisappeared(Channel channel, Member member) throws Exception {
		LOGGER.error("[DISAPPEARED " + member + "]");
		// channel.send(member, "失败.".getBytes());
	}
	
	/**
	 * Print members...
	 * @param channel
	 * @since 1.0
	 */
	public void printMembers(Channel channel) {
		Member[] members = channel.getMembers();
		for (Member member_ : members) {
			System.out.println(member_);
		}
	}

}
